/**
 * zmn产品模块-产品列表
 * @author tanghongjun
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    zmnLayUtil.layFormVerify(form);
    var data = {"code": 0, "msg": "", "count": json.totalCount, "items": json.items};
    var cols = [
        [
            {field: 'productId', type: 'checkbox', fixed: 'left', align: 'center'},
            {field: 'productId', title: 'ID', align: 'center', unresize: true, sort: false},
            {field: 'servCategName', title: '服务分类', align: 'center', unresize: true, sort: false},
            {field: 'categOneName', title: '一级分类', align: 'center', unresize: true, sort: false},
            {field: 'categName', title: '二级分类', align: 'center'},
            // { field:'ficoCtegName', title:'绩效分类', width:'150', align:'center', unresize:true,sort:false},
            {field: 'name', title: '产品名称', tips: 'name', align: 'center'},
            // { field:'unit', title:'单位', width:'150', align:'center', unresize:true,sort:false},
            // { field:'priceType', title:'服务价格类型', width:'150', align:'center', unresize:true, templet: function(d) {
            //         return zmnEnum.PRICE_TYPE[d.priceType];
            //     }},
            // { field:'status', title:'状态', width:'100', align:'center', unresize:true,templet: function(d) {
            //         return zmnEnum.STATUS[d.status];
            //     }},

        ]
    ];

    zmnProduct.laySelectSpecial(form, "/activity/config/listChilds.action", "/activity/config/listCateChilds.action", "servCategId", "categOneId", "categId");

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-act-modify-product-add-table)", function (obj) {
        ids = table.checkStatus('zmn-act-modify-product-add-table').data.map((item) => item.productId);
    });

    let query;
    form.on('submit(query-submit)', function (obj) {
        tableRender(obj.field);
        query = obj.field;
        return false;
    });

    let tableRender = function (data) {
        // let url = "/product/loadData.action";
        let url = '/activity/config/list-product.action'
        zmnLayUtil.layTableRender(table, cols, "zmn-act-modify-product-add-table", data, url)
    };

    tableRender({status: 2});

    //新增活动和产品的关联
    $('body').on('click', '#act-product-add', function () {
        if (ids.length === 0) {
            return layer.msg("请选择至少1个产品！", {icon: 5, shift: 6});
        }
        var data = {productIds: ids, activityId: json.activityId};
        $.ajax({
            type: "POST",
            url: "/activity/config/act-product-add",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (res) {
                if (res.status == 200) {
                    zmnLayUtil.closeThisIframeMsg(parent, res.message, "zmn-act-modify-product-add-table");
                    return false;
                } else {
                    layer.msg(res.message, {icon: 5, shift: 6});
                }
            }
        });
    });

    //对外暴露的接口
    exports('zmn_plat_activity_modifyProduct_add', {});
});

var zmnProduct = {
    /**
     * 多级下拉框联动
     * @param form layui.form
     * @param url1 selectOneId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param url1 selectTwoId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param selectOneId 第1级select的ID，不含#号
     * @param selectTwoId 第2级select的ID，不含#号
     * @param selectThrId 第3级select的ID，不含#号，如无第3级则两级联动
     */
    laySelectSpecial: function (form, url1, url2, selectOneId, selectTwoId, selectThrId) {
        let $ = layui.$;
        let servCategId;
        let categOneId;
        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            servCategId = obj.value;
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: {servCategId: servCategId},
                success: function (res) {
                    var items = res.items;
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i].categId + '">' + items[i].name + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    form.render("select");
                }
            });
        });
        if (zmnUtil.isEmptyNull(selectThrId)) {
            return;
        };

        form.on("select(" + selectTwoId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            categOneId = obj.value;
            $.ajax({
                type: "GET", url: url2,
                dataType: "json", data: {categOneId: categOneId},
                success: function (res) {
                    var items = res.items;
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i].categId + '">' + items[i].name + '</option>';
                    }
                    $("#" + selectThrId).html(optionHtml);
                    form.render("select");
                }
            });
        });
    },
}
